//
//  GridCollectionViewCell.m
//  GuoMei
//
//  Created by 王磊 on 2018/3/6.
//  Copyright © 2018年 王磊. All rights reserved.
//

#import "GridCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "Masonry.h"
@interface GridCollectionViewCell()
@property (strong , nonatomic)UIImageView * gridImageView;
@property (strong , nonatomic)UILabel * titleLabel;
@property (strong , nonatomic)UILabel * tagLabel;
@end
@implementation GridCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
   self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}
-(void)setUpUI{
    _gridImageView = [[UIImageView alloc]init];
    _gridImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_gridImageView];
    
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.font = PFR13Font;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    
    _tagLabel = [[UILabel alloc]init];
    _tagLabel.font = [UIFont systemFontOfSize:8];
    _tagLabel.backgroundColor = [UIColor whiteColor];
    _tagLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_tagLabel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_gridImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.top.mas_equalTo(self)setOffset:10];
//        if (iphone5) {
//            make.size.mas_equalTo(CGSizeMake(38, 38));
//        }else{
            make.size.mas_equalTo(CGSizeMake(45, 45));
//        }
        make.centerX.mas_equalTo(self);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        [make.top.mas_equalTo(_gridImageView.mas_bottom)setOffset:5];
    }];
    
    
    
    [_tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_gridImageView.mas_centerX);
        make.top.mas_equalTo(_gridImageView);
        make.size.mas_equalTo(CGSizeMake(35, 15));
    }];
    
}

//-(void)setGridItem:(gridModel *)gridItem{
//    _gridItem = gridItem;
//
//    _titleLabel.text = gridItem.gridTitle;
//    _tagLabel.text = gridItem.gridTag;
//    if (gridItem.gridTag.length == 0) {
//        _tagLabel.hidden = YES;
//    }
//
//    [_gridImageView sd_setImageWithURL:[NSURL URLWithString:gridItem.iconImage] placeholderImage:nil];
//}


- (void)setGridItem:(gridModel *)gridItem
{
    NSLog(@"111%@",gridItem);
    _gridItem = gridItem;
    
    
    _titleLabel.text = gridItem.gridTitle;
    _tagLabel.text = gridItem.gridTag;
    
    _tagLabel.hidden = (gridItem.gridTag.length == 0) ? YES : NO;
    
    if (_gridItem.iconImage.length == 0) return;
    if ([[_gridItem.iconImage substringToIndex:4] isEqualToString:@"http"]) {
        
        [_gridImageView sd_setImageWithURL:[NSURL URLWithString:gridItem.iconImage]placeholderImage:[UIImage imageNamed:@"default_49_11"]];
    }else{
        _gridImageView.image = [UIImage imageNamed:_gridItem.iconImage];
    }
}




@end
